package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.google.gson.Gson;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.UserInfoBean;
import com.example.wanbaqian.customView.BottomAreaDialog;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

/**
 * 代理申请界面
 */
public class FindApplyOneActivity extends BaseActivity implements HttpCallback {


    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_position)
    TextView edPosition;
    @BindView(R.id.ed_)
    EditText ed;
    @BindView(R.id.btn_ok)
    Button btnOk;
    private BottomAreaDialog dialog;

    @Override
    protected int getLayout() {
        return R.layout.activity_findapplyone;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setToolBar("代理");
        setStatusBarColor(R.color.colorBackground);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
        dialog = new BottomAreaDialog(mContext);
        dialog.setOnAddressSelectedListener(listener);
        dialog.getCity();
    }

    OnAddressSelectedListener listener = new OnAddressSelectedListener() {
        @Override
        public void onAddressSelected(Province province, City city, County county, Street street) {
            if (county != null) {
                edPosition.setText(city.name + county.name);
                edPosition.setTag(county.id);
                dialog.dismiss();
            }
        }
    };

    @OnClick(R.id.ed_position)
    public void edPosition() {
        dialog.show();
    }

    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        if (edName.getText().toString().length() < 2 || edName.getText().toString().length() > 4) {
            showToast("请填写正确的姓名");
            return;
        } else if (edName.getText().toString().length() == 11) {
            showToast("请填写正确的手机号");
            return;
        } else if (edPosition.getText().toString().isEmpty()) {
            showToast("请选择代理省市区");
            return;
        } else if (ed.getText().toString().isEmpty()) {
            showToast("请填写代理优势");
            return;
        } else {
            HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                            .getRequest(HttpURL.AGENT_APPLY, mContext,
                                    new String[]{"accessid", "user_name", "user_mobile", "area_no", "apply_remark"}
                                    , new Object[]{SPUtils.getAccessid(mContext), edName.getText().toString(), edPhone.getText().toString(), edPosition.getTag().toString(), ed.getText().toString()})
                    , mContext, null, CommonValue.REQUEST_CODE1, this);
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        UserInfoBean infoBean = SPUtils.getUserInfo(mContext);
        infoBean.setAgent_status(1);
        SPUtils.setUserInfo(mContext, new Gson().toJson(infoBean));
        openActivity(FindApplyTwoActivity.class);
        finish();
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
